﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Extensions;
using VOL.Entity.DomainModels;
using VOL.SmartHealthcare.Infrastructure;
using VOL.Team.IRepositories;
using VOL.Team.TeamIservices.MaterialManagement.IServices;

namespace VOL.Team.TeamIservices.MaterialManagement.Services
{
    /// <summary>
    /// BOM实现类---bpy
    /// </summary>
    public class BomServices: IBomServices
    {
        /// <summary>
        /// BOM表仓储
        /// </summary>
        private readonly IBaseRepository<BOM> _bomBaseRepository;
        /// <summary>
        /// 物料表仓储
        /// </summary>
        private readonly IBaseRepository<MaterialPicking> _materialPickingBaseRepository;
        public BomServices(IBaseRepository<BOM> bomBaseRepository, IBaseRepository<MaterialPicking> materialPickingBaseRepository)
        {
            _bomBaseRepository = bomBaseRepository;
            _materialPickingBaseRepository = materialPickingBaseRepository;
        }
        /// <summary>
        /// 新增BOM
        /// </summary>
        /// <param name="bOM">要添加的实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddBomAsync(BOM bOM)
        {
            try
            {
                int i = await _bomBaseRepository.AddAsync(bOM);
                return i;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 修改BOM
        /// </summary>
        /// <param name="bOM">要添加的实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateBomAsync(BOM bOM)
        {
            try
            {
                int i = await _bomBaseRepository.UpdateAsync(bOM);
                return i;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 获取物料包数据
        /// </summary>
        /// <returns>返回物料信息</returns>
        public IQueryable<MaterialPicking> GetMaterialPicking()
        {
            try
            {
                var materialPicking = _materialPickingBaseRepository.GetAll();
                return materialPicking;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取BOM数据
        /// </summary>
        /// <returns>返回BOM信息</returns>
        public IQueryable<BOM> GetBom(string? materialCode,string? bomCode)
        {
            try
            {
                var bOM = _bomBaseRepository.GetAll();
                bOM = bOM.WhereIF(!string.IsNullOrEmpty(materialCode), x => x.MaterialCode.Contains(materialCode));
                bOM = bOM.WhereIF(!string.IsNullOrEmpty(bomCode), x => x.BOMCode.Contains(bomCode));
                return bOM;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 删除BOM
        /// </summary>
        /// <param name="bOM">要删除数据的id</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeleteBomAsync(string id)
        {
            try
            {
                string []idArr =id.Split(',');
                int i = await _bomBaseRepository.DeletteRangeAsync(x=> idArr.Contains(Convert.ToString(x.Id)));
                return i;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
